package newhiant.hianto2o.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import newhiant.hianto2o.com.databinding.ItemRecyclerActivityBinding;
import newhiant.hianto2o.com.model.Member;

/**
 * Created by Administrator on 2017/8/2 0002.
 */

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHoldView> {

    private Context mContext;
    private List<Member> members;

    public RvAdapter(Context mContext, List<Member> members) {
        this.mContext = mContext;
        this.members = members;
    }

    @Override
    public RvHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        return RvHoldView.create(LayoutInflater.from(mContext),parent);
    }

    @Override
    public void onBindViewHolder(RvHoldView holder, int position) {
            holder.bindTo(members.get(position));
    }


    @Override
    public int getItemCount() {
        return members == null ? 0 :members.size();
    }

    static class RvHoldView extends RecyclerView.ViewHolder {

        // 创建一个静态获取方法
        static RvHoldView create(LayoutInflater inflater, ViewGroup parent) {
            ItemRecyclerActivityBinding binding = ItemRecyclerActivityBinding.inflate(inflater, parent, false);
            return new RvHoldView(binding);
        }

        ItemRecyclerActivityBinding mBinding;

        public RvHoldView(ItemRecyclerActivityBinding binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }

        public void bindTo(Member member) {
            mBinding.setMember(member);
            // 它使数据绑定刷新所有挂起的更改。这官方的解释好难懂，其实功能就是让数据立即展示在布局上
            mBinding.executePendingBindings();
        }
    }
}
